package com.ycd._02_xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class _01_producer {

    public static void main(String[] args) throws Exception {
        // 1.获取producer-生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_group");
        // 2.设置nameserver连接
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动
        producer.start();
        // 4.创建消息对象-设置消息
        Message message = new Message("xhr", "dddd", "今天星期二".getBytes(StandardCharsets.UTF_8));

        // 异步发送没有返回值-它是通过回调的方式来获取到消息的发送结果
        producer.send(message, new SendCallback() {
            // //发送成功结果处理
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("成功消息!!" + sendResult.getMsgId());
            }
            //发送异常结果处理
            @Override
            public void onException(Throwable throwable) {
                System.out.println("异常消息" + throwable.getMessage());

            }
        });

        System.out.println("111");
        // 5.关闭
        /*producer.shutdown();*/
    }
}
